define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'activity.prize/index' + location.search,
                    add_url: 'activity.prize/add/activity_id/',
                    edit_url: 'activity.prize/edit',
                    del_url: 'activity.prize/del',
                    multi_url: 'activity.prize/multi',
                    import_url: 'activity.prize/import',
                    table: 'activity_prize',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2'),"3":__('Type 3'),"4":__('Type 4')}, formatter: Table.api.formatter.normal},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'min_price', title: __('Min_price'), operate:'BETWEEN', operate: false},
                        {field: 'max_price', title: __('Max_price'), operate:'BETWEEN', operate: false},
                        {field: 'number', title: __('Number'), operate: false},
                        {field: 'available_number', title: __('Available_number'), operate: false},
                        {field: 'prize_probability', title: __('Prize_probability'), operate: false},
                        {field: 'status', title: __('Status'), searchList: {"normal":__('Normal'),"hidden":__('Hidden')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'activity.name', title: __('Activity.name'), operate: false},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                queryParams: function(params){
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    var activity_id = Fast.api.query('ids');
                    $.fn.bootstrapTable.defaults.extend.add_url=$.fn.bootstrapTable.defaults.extend.add_url+activity_id;
                    if (activity_id != null){
                        filter['activity_id']=Fast.api.query('ids');
                        op['activity_id'] = '=';
                    }
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
